/**
 * 建议尽量全字段或者区域多字段进行匹配, 避免误触
 */
// Import("ocr.js")
// Import("utils.js")
// Import("ggUtils.js")


// SystemPrint.log(app.getTopPackName())
var APP_NAME_BDJSB = '百度极速版'
var PACK_NAME_BDJSB = 'com.baidu.searchbox.lite'
var MAIN_PAGE_FLAG_BDJSB = '百度'
var ENTER_CENTER_PAGE_FLAG_BDJSB = '进入任务中心的标志位'
var CENTER_PAGE_FLAG_BDJSB = '去提现'

function skip_主界面的弹窗_BDJSB() {
    // ocrTextTrue('仅使用期间')
    agentOrHidClick(ocrText('禁止', '仅使用期间', '不允许', '用期间允许'))
    // agentClickLeft(ocrText('不再提示'))

    var detectsclose = opencv.findImagesEx('图色634966.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }

    var detectsclose2 = opencv.findImagesEx('图色646766.cv');
    if (detectsclose2 != null) {
        agentOrHidClick(detectsclose2[0])
    }
}

function skip_任务中心的弹窗_BDJSB() {
    agentOrHidClick(ocrTextMoreInCenter('去添加', '直接领取'))
    agentOrHidClick(ocrTextMoreComplete('添加', '取消', '拒绝'))

    var ggFlag = ocrTextMoreInCenter('看广告再得', '看广告立即领特权')
    if (getDectNumber(ggFlag) >= 199) {
        if (!isEnableSwitch('bdjs_newnode')) {
            watchGG_多次看广告(ocrTextMoreInCenter('看广告再得', '看广告立即领特权'))
        }

    }

    //找图返回目标数组
    var detectsclose = opencv.findImagesEx('图色332984.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }

}

function back2center_BDJSB() {
    if (!backUntilFindView(() => ocrText('金币收益', '新用户', '开宝箱', '限时福利'))) {
        // enter_任务中心_BDJSB(true)
    }
    // array.forEach(element => {
    //     // Ddddd
    // });
}

// 启动任务
function start_app_BDJSB() {
    // app.closeApp(PACK_NAME_BDJSB)
    // sleep.millisecond(200)
    printinfoOld('start_app_BDJSB')

    var isuccess = startApp(PACK_NAME_BDJSB)
    if (!isuccess) {
        return false
    }

    // 跳过首页广告
    expectView(() => {
        return ocrTextMoreInRegion(0, 0.88, 1, 1, MAIN_PAGE_FLAG_BDJSB, '我的')
    }, (times) => {
        skip_主界面的弹窗_BDJSB()

        if (times > 5) {
            agentOrHidback()
            sleep.millisecond(500)
            // todo
        }
        sleep.millisecond(500)
    }, (findView) => {
        // agentOrHidClick(findView)
    }, null, 6)

    return true
}

// 进入任务中心
function enter_任务中心_BDJSB(iskip) {
    printinfoOld('enter_任务中心_BDJSB')

    if (iskip == undefined) iskip = false

    // 进入任务中心
    expectView(() => {
        //找图返回目标数组
        var detects福利中心 = opencv.findImagesEx('图色424490.cv');
        if (detects福利中心 != null) {
            return detects福利中心[0]
        }
        return null
    }, (times) => {
        skip_主界面的弹窗_BDJSB()

        sleep.millisecond(500)
        SystemPrint.warn('未找到主界面期望标志位')
    }, (findView, stepTimes) => {
        if (stepTimes == 1) {
            skip_主界面的弹窗_BDJSB()
        }
        agentOrHidClick(findView)
    }, () => {
        // 失败后的方案 或者其它方案进入
        clickOffectY(0.5, 1, 60)
    }, 3)


    if (!iskip) {
        // 期望标志位
        expectView(() => {
            return ocrText(CENTER_PAGE_FLAG_BDJSB, '金币收益', '任务完成', '看广告', '开宝箱')
        }, (times) => {
            skip_任务中心的弹窗_BDJSB()

            if (times > 5) {
                agentOrHidback()
                sleep.millisecond(500)
                // todo 未找到后 返回怎么做
            }
            sleep.millisecond(500)
            SystemPrint.warn('未找到任务界面期望标志位')
        }, (findView) => {
            // agentOrHidClick(findView)
        }, null, 6)
    }
}


function open_签到_BDJSB() {
    printinfoOld('open_签到_BDJSB-------------------------------')
    // skip_任务中心的弹窗_BDJSB()

    if (hasExcuteFunc('open_签到_BDJSB') || getExcuteFuncTimes('open_签到_BDJSB') > 3) {
        return
    }

    if (getExcuteFuncTimes('open_签到_BDJSB') <= 1) {
        agentOrHidClick(ocrText('开心收下'))
        var flag = ocrWaitForMoreTextMillisecond('连签排行榜', '重新开始', '签到中断', '已连续签到', 8)
        if (flag == null) {
            skip_任务中心的弹窗_BDJSB()
            flag = ocrWaitForMoreTextMillisecond('连签排行榜', '重新开始', '签到中断', '已连续签到', 3)
        }
        agentOrHidClick(ocrText('开心收下'))
    }

    agentOrHidClick(ocrText('立即领取', '直接领取', '直接领', '接领取'), 2, () => {
        if (ocrText('我知道了', '去睡觉')) {
            setExcuteFunc('open_签到_BDJSB')
        }
    })

    addExcuteFuncTimes('open_签到_BDJSB')

    var detectsclose = opencv.findImagesEx('图色801752.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }

    skip_任务中心的弹窗_BDJSB()
}

function open_开宝箱_BDJSB() {
    printinfoOld('open_开宝箱_BDJSB-------------------------------')
    if (isEnableSwitch('bdjs_newnode')) {
        return
    }
    skip_任务中心的弹窗_BDJSB()

    if (getExcuteFuncTimes('open_开宝箱_BDJSB_fail') > 2) {
        return
    }

    agentOrHidClick(ocrText('开宝箱得金币'), 1, () => {
        // ocrText('看视频', true)
        // watchGG_多次看广告(ocrTextMoreInRegion(0, 0.5, 1, 0.85, '观看视频', '看视频', '看广告'))

        // ocrTextTrue('看视频')
        var flagmore = ocrTextMoreInRegion(0, 0.5, 1, 0.85, '看视频再', '看广告再')
        if (getDectNumber(flagmore) > 198) {
            watchGG_多次看广告(flagmore)
        } else {
            addExcuteFuncTimes('open_开宝箱_BDJSB_fail')
        }

        skip_任务中心的弹窗_BDJSB()

        //找图返回目标数组
        var detectsclose = opencv.findImagesEx('图色411318.cv');
        if (detectsclose != null) {
            agentOrHidClick(detectsclose[0])
        }
    })
}

function watch_看广告_BDJSB() {
    printinfoOld('watch_看广告_BDJSB-------------------------------')
    // if (!isEnableSwitch('bd_kspgg')) {
    //     return
    // }
    if (isEnableSwitch('bdjs_newnode')) {
        return
    }

    if (hasExcuteFunc('watch_看广告_BDJSB') || getExcuteFuncTimes('watch_看广告_BDJSB' > 6)) {
        return
    }

    skip_任务中心的弹窗_BDJSB()

    move2Top(7, 300, '金币收益', '现金收益')
    moveUp2FindWidTxtMore((findView) => {
        if (getDectNumber(findView) > 198) {
            agentOrHidClickAbsoultXOffectY(findView, screen.percentToWidth(1) - 200, -30)
            watchGG_多次看广告(findView, true)
        } else {
            // setExcuteFunc
            addExcuteFuncTimes('watch_看广告_BDJSB')
        }
    }, '看广告赚钱', () => {
        return ocrText('活动规则', '问题反馈')
    })
}

function enter_视频界面__BDJSB() {
    // skip_主界面的弹窗_BDJSB()
    skip_主界面的弹窗_BDJSB()
    clickOffectY(0.1, 1, 60)

    expectView(() => {
        return ocrText('搜索')
    }, () => {
        skip_主界面的弹窗_BDJSB()
        clickOffectY(0.1, 1, 60)
        sleep.second(1)
    })

    skip_主界面的弹窗_BDJSB()
    // var ocr1短剧 = opencv.OCREx('图色837824.cv');
    // if ((ocr1短剧 != null && ocr1短剧.getAllString() == '短剧')) {
    //     agentOrHidClick(ocr1短剧.getAllDetect()[0])
    //     sleep.second(2)
    //     skip_主界面的弹窗_BDJSB()
    //     agentOrHidClick(ocrWaitForMoreTextMillisecond('立即观看', '观看', '集全', 10))

    //     if (ocrText('浏览历史', '立即观看')) {
    //         agentOrHidClickAbsoultX(ocrText('浏览历史', '立即观看'), screen.percentToWidth(0.25))
    //     }


    // } else {
    //     //找图返回目标数组

    // }

    var detects视频 = opencv.findImagesEx('图色616507.cv');
    if (detects视频 != null) {
        agentOrHidClick(detects视频[0])
    } else {
        clickOffectY(3 / 10, 1, 60)
    }

    // var flag短剧 = ocrWaitForTextMillisecondInRegion(0, 0, 1, 0.2, '短剧', 6)
    // if (flag短剧) {
    //     printinfoOld('观看短剧')
    //     agentOrHidClick(flag短剧)
    //     agentOrHidClick(ocrText('上滑观看'))


    // } else {

    // }

    if (ocrTextMoreInCenter('立即领今日打款', '看视频7天', '中断签到')) {
        agentOrHidback()
        sleep.second(1)

        clickOffectY(1 / 10, 1, 60)

        //         //找图返回目标数组
        // var detects=opencv.findImagesEx('图色709514.cv');
        // if(detects!=null){
        //    printl(detects);
        //    detects[0].click();
        // }
        moveUp2FindWid((findView) => {
            agentOrHidClickAbsoultXOffectY(findView, screen.percentToWidth(0.5), 150)
        }, '万播放')
    }

}


// start_看小说任务_BDJSB()
function start_看小说任务_BDJSB() {
    printinfoOld('start_看小说任务_BDJSB')

    if (hasExcuteFunc('start_看小说任务_BDJSB')) {
        return
    }

    if (!start_app_BDJSB()) {
        return
    }

    skip_主界面的弹窗_BDJSB()

    var watch小书 = opencv.OCREx('图色857416.cv');
    if (watch小书 != null) {
        agentOrHidClick(watch小书.getAllDetect()[0])
    } else {
        clickOffectY(7 / 10, 1, 60)
        sleep.second(2)
        if (ocrText('我的现金', '立即提现', '签到领红包')) {
            +
                setExcuteFunc('start_看小说任务_BDJSB')
            stopAllApp()
            return
        }
    }

    skip_主界面的弹窗_BDJSB()
    agentOrHidClick(ocrWaitForMoreTextMillisecond('完结榜', 10))
    skip_主界面的弹窗_BDJSB()
    agentOrHidClick(ocrWaitForMoreTextMillisecond('万热度'))
    agentOrHidClick(ocrWaitForMoreTextMillisecond('免费阅读', '继续阅读', 10))

    if (ocrText('夜间', '目录')) {
        click(0.5, 0.5)
    }

    var clickfalg = ocrText('设置里', '底部', '功能提醒', '为了更好', '广告为')

    startThredTaskMaxTime超时结束(() => {
        whilestep((steptimes, alltimes, canbarek) => {
            if (steptimes % 6 == 0) {
                agentOrHidClick(ocrText('我知道了', '知道了', '免费阅读', '继续阅读'))
            }
            if (steptimes % 12 == 0) {
                if (ocrText('赚更多')) {
                    setExcuteFunc('start_看小说任务_BDJSB')
                    canbarek()
                }
            }
            if (clickfalg == null) {
                clickOffectX(1, 0.05, 1)
            } else {
                agentOrHidClickAbsoultX(clickfalg, screen.percentToWidth(1))
            }
        }, ENV() == 'test' ? 3 * 60 : 10 * 59)
    }, 17 * 60)


    if (isEnableSwitch('bdjs_newnode')) {
        if (getExcuteFuncTimes('start_看小说任务_BDJSB') >= 15) {
            setExcuteFunc('start_看小说任务_BDJSB')
        }
    } else {
        if (getExcuteFuncTimes('start_看小说任务_BDJSB') >= 6) {
            setExcuteFunc('start_看小说任务_BDJSB')
        }
    }
    addExcuteFuncTimes('start_看小说任务_BDJSB')

    stopAllApp()
}

// 进行浏览任务
// start_浏览任务_BDJSB()
function start_浏览任务_BDJSB() {
    printinfoOld('start_浏览任务_BDJSB')

    // SystemPrint.log(hasExcuteFunc('start_浏览任务_BDJSB'))
    if (hasExcuteFunc('start_浏览任务_BDJSB')) {
        return
    }
    if (getExcuteFuncTimes('start_浏览任务_BDJSB') > 2 && !isEnableSwitch('bdjs_newnode')) {
        return
    }

    if (!start_app_BDJSB()) {
        return
    }

    // enter_视频界面__BDJSB()
    skip_主界面的弹窗_BDJSB()
    clickOffectY(0.1, 1, 60)

    expectView(() => {
        return ocrText('搜索')
    }, () => {
        skip_主界面的弹窗_BDJSB()
        clickOffectY(0.1, 1, 60)
        sleep.second(1)
    })

    skip_主界面的弹窗_BDJSB()

    startThredTask(() => {
        agentSwipSmallDowner(false, 1)
        sleep.second(5)
        agentSwipSmallDowner(false, 1)
        sleep.second(3)

        if (isEnableSwitch('bdjs_newnode')) {
            if (getExcuteFuncTimes('start_浏览任务_BDJSB') > 5) {
                setExcuteFunc('start_浏览任务_BDJSB')
                // setExcuteFunc('start_浏览任务_BDJSB', false)
            }
        } else {
            if (getExcuteFuncTimes('start_浏览任务_BDJSB') > 2) {
                setExcuteFunc('start_浏览任务_BDJSB')
                // setExcuteFunc('start_浏览任务_BDJSB', false)
            }
        }
    }, (stepTimes) => {
        // 跳过图文

        if (stepTimes % 2 == 0) {
            agentSwipSmallDowner()
            // agentSwipSmallerDown很小间距()

        } else {
            // agentSwipSmallerUp很小间距()
            agentSwipSmallUper()
        }

        if (stepTimes % 12 == 0) {
            agentOrHidClick(ocrText('忽略提醒', '取消'))
            var detectsclose = opencv.findImagesEx('图色339083.cv');
            if (detectsclose != null) {
                agentOrHidClick(detectsclose[0])
            }

            //找图返回目标数组
            var detectsclose2 = opencv.findImagesEx('图色717953.cv');
            if (detectsclose2 != null) {
                agentOrHidClick(detectsclose2[0])
            }
        }

        if (stepTimes % 36 == 0) {
            skip_主界面的弹窗_BDJSB()
        }

    }, 15 * 60)

    addExcuteFuncTimes('start_浏览任务_BDJSB')

    // // 等待红包界面
    // expectView(() => {
    //     //找色返回目标数组
    //     var detects = opencv.findMultiColorEx('图色710311.cv');
    //     if (detects != null) {
    //         return detects[0]
    //     }

    //     var detects福利中心 = opencv.findImagesEx('图色424490.cv');
    //     if (detects福利中心 != null) {
    //         return detects福利中心[0]
    //     }

    //     // ocrTextTrue('短剧·')
    //     return ocrText('更新至', '浏览历史', '立即观看', '短剧·')
    // }, (times) => {
    //     sleep.millisecond(1000)
    //     if (times > 3) {
    //         skip_主界面的弹窗_BDJSB()
    //     }
    // }, (findView) => {
    //     if (isEnableSwitch('bdjs_newnode')) {
    //         if (getExcuteFuncTimes('start_浏览任务_BDJSB') > 8) {
    //             setExcuteFunc('start_浏览任务_BDJSB')
    //             // setExcuteFunc('start_浏览任务_BDJSB', false)
    //         }
    //     } else {
    //         if (getExcuteFuncTimes('start_浏览任务_BDJSB') > 5) {
    //             setExcuteFunc('start_浏览任务_BDJSB')
    //             // setExcuteFunc('start_浏览任务_BDJSB', false)
    //         }
    //     }


    //     addExcuteFuncTimes('start_浏览任务_BDJSB')

    //     startThredTask(() => {
    //     }, (stepTimes) => {
    //         // 跳过图文
    //         if (stepTimes % 3 == 0) {
    //             if (ocrText('直播间', '直播中', '进入直播间')) {
    //                 agentSwipBiger()
    //                 sleep.second(1)
    //             }
    //         }


    //         if (stepTimes % 5 == 0 || stepTimes % 8 == 0) {
    //             // 播放进度
    //             var detects = opencv.findImagesEx('图色372988.cv');
    //             if (detects != null) {

    //                 // printinfoOld(detects[0].getRect());
    //                 var findRect = detects[0].getRect()
    //                 if (findRect.getCenterX() > 0.9) {
    //                     printinfoOld('视频快播放完了,下一条')
    //                     agentSwipBiger()
    //                     sleep.second(1)
    //                 }
    //             }


    //             if (ocrText('上滑继续', '播放下一')) {
    //                 agentSwipBiger()
    //                 sleep.second(1)
    //             }

    //             // ocrTextTrue('取消')
    //             agentOrHidClick(ocrText('忽略提醒', '取消'))
    //         }

    //         if (stepTimes % 12 == 0) {
    //             var detectsclose = opencv.findImagesEx('图色339083.cv');
    //             if (detectsclose != null) {
    //                 agentOrHidClick(detectsclose[0])
    //             }
    //         }

    //         if (stepTimes % 12 == 0) {
    //             skip_主界面的弹窗_BDJSB()
    //             agentOrHidClick(ocrText('立即观看', '集全'))
    //             agentOrHidClickOffectY(ocrText('浏览历史'), -200)
    //         }
    //     }, 10 * 60)
    // }, () => {
    //     // enter_视频界面__BDJSB()
    // }, 6)

    stopAllApp()

}

// start_收集金币_BDJSB()
function start_收集金币_BDJSB() {
    printinfoOld('start_收集金币_BDJSB')
    if (!isEnableSwitch('bdjsb_sjjb')) {
        return
    }
    if (getExcuteFuncTimes('start_收集金币_BDJSB') > 5) {
        return
    }
    currentAppPackName = PACK_NAME_BDJSB
    // closeApp(PACK_NAME_BDJSB)

    if (!start_app_BDJSB()) {
        return
    }

    enter_任务中心_BDJSB()
    if (getExcuteFuncTimes('start_收集金币_BDJSB') <= 1) {
        if (ocrWaitForMoreTextMillisecond('可以提现', '微信提现')) {
            skip_任务中心的弹窗_BDJSB()
        }
    }

    startThredTaskMaxTime超时结束(() => {
        open_签到_BDJSB()

        open_开宝箱_BDJSB()

        watch_看广告_BDJSB()
        var haswatchtime = ocrText('已看')
        if (getDectNumber(haswatchtime) >= 60) {
            setExcuteFunc('start_浏览任务_BDJSB')
        }

        move2Top(2)
        if (getDectNumber(ocrText('已看')) >= 60) {
            setExcuteFunc('start_浏览任务_BDJSB')
        }
        if (getExcuteFuncTimes('start_收集金币_BDJSB') >= 3) {
            var flag = ocrText('可领')
            agentOrHidClick(flag)
            flag = ocrText('可领')
            agentOrHidClick(flag)

            for (var i = 0; i < 10; i++) {
                agentSwipSmallLeft(ocrText('已看'), -250)
                sleep.second(1)
                agentOrHidClick(ocrText('可领'), 5)
            }

            if (isEnableSwitch('bdjsb_autotx') && !hasExcuteFunc('bdjsb_tx')) {
                agentOrHidClick(ocrText('去提现'))
                agentOrHidClick(ocrWaitForText('0.3元'))

                // ocrTextTrue('账号')
                agentOrHidClickOffectY(ocrWaitForText('换账号', '切换'), 200, 2, () => {
                    setExcuteFunc('bdjsb_tx')
                })
            }
        }


    }, 9 * 60)

    addExcuteFuncTimes('start_收集金币_BDJSB')

    stopAllApp()
    // closeApp(PACK_NAME_BDJSB)
}